// Table 3: Effects of road tolls and bus lane distance on car ownership,
// 2015-2017. Conditional on residential and work location in 2008

*===============================================================================
// Step 1: Load data
*===============================================================================
use    "${dataout}MainDataset" , clear
keep if (year>=2015 & year<=2017) | year == 2008

keep if couple == 1

// Residence and work location defined by basic stastictical units (BSU)
foreach var in grkrets_num grk_bed1_num grk_bed2_num { 	// For residence, work1 and work2 BSU ...
	gen long aux = `var' if year == 2008 				// Identify BSU in 2008
	bys familienr: egen long `var'_2008 = mean(aux) 	// Fill in for remaining years
	drop aux	
} 

gen grk_nochange = (grkrets_num == grkrets_num_2008) 						// Household lives in same BSU as in 2008
gen grk_bed1_nochange = (grk_bed1_num == grk_bed1_num_2008) 				// Person 1 works in same BSU as in 2008
gen grk_bed2_nochange = (grk_bed2_num == grk_bed2_num_2008) 				// Person 2 works in same BSU as in 2008
gen grk_bed_nochange = (grk_bed1_nochange == 1 & grk_bed2_nochange == 1) 	// Both work in same BSU as in 2008
compress
drop if year == 2008 // Done with 2008 data, drop it from dataset again

*===============================================================================
// Step 2: Run regression, save results as ster files
*===============================================================================
** Select variables to loop over: 
local outcomelist BEV_fam_yes ICE_fam_count 
local treatvar1list toll_fam_mean          
local treatvar2list ptl_fam_km_mean     

** Regressions - loop over: outcome, treatment1, treatment2
foreach outcome   in `outcomelist'  {
	foreach treatvar1 in `treatvar1list'{
		foreach treatvar2 in `treatvar2list'{
			
			* (1) Baseline regression
			reghdfe `outcome' `treatvar1' `treatvar2' ///
				$age $distance $time $publictime $publicquality ///
				if grkrets_num_2008 != ., ///
				absorb($FE $household $income $employment $education i.year) ///
				vce(cluster $clustervar) 
			qui summarize BEV_fam_yes 		if e(sample)==1
			estadd scalar MeanDep1 = 		r(mean), replace
			qui summarize ICE_fam_count 	if e(sample)==1
			estadd scalar MeanDep2 = 		r(mean), replace
			qui summarize `treatvar1' 		if e(sample)==1
			estadd scalar Treat1 = 			r(mean), replace
			qui summarize `treatvar2' 		if e(sample)==1
			estadd scalar Treat2 = 			r(mean), replace
			estadd local year=				"2015-17" , replace
			estadd local res_same=			"\checkmark", replace
			estadd local work_same=			"\checkmark", replace
			estadd local res_different=		"\checkmark", replace
			estadd local work_different=	"\checkmark", replace
			*eststo table3_`outcome'_reg2
			estimates save "${ster}table3_`outcome'_reg1" , replace

			* (2) Same residential location
			reghdfe `outcome' `treatvar1' `treatvar2' ///
				$age $distance $time $publictime $publicquality ///
				if grk_nochange == 1, ///
				absorb($FE $household $income $employment $education i.year) ///
				vce(cluster $clustervar) 
			qui summarize BEV_fam_yes 		if e(sample)==1
			estadd scalar MeanDep1 = 		r(mean), replace
			qui summarize ICE_fam_count 	if e(sample)==1
			estadd scalar MeanDep2 = 		r(mean), replace
			qui summarize `treatvar1' 		if e(sample)==1
			estadd scalar Treat1 = 			r(mean), replace
			qui summarize `treatvar2' 		if e(sample)==1
			estadd scalar Treat2 = 			r(mean), replace
			estadd local year=				"2015-17" , replace
			estadd local res_same=			"\checkmark", replace
			estadd local work_same=			"\checkmark", replace
			estadd local res_different=		"\checkmark", replace
			estadd local work_different=	"\checkmark", replace
			*eststo table3_`outcome'_reg2
			estimates save "${ster}table3_`outcome'_reg2" , replace

			* (3) Different residential location
			reghdfe `outcome' `treatvar1' `treatvar2' ///
				$age $distance $time $publictime $publicquality ///
				if grkrets_num_2008 != . & grk_nochange == 0, ///
				absorb($FE $household $income $employment $education i.year) ///
				vce(cluster $clustervar) 
			qui summarize BEV_fam_yes 		if e(sample)==1
			estadd scalar MeanDep1 = 		r(mean), replace
			qui summarize ICE_fam_count 	if e(sample)==1
			estadd scalar MeanDep2 = 		r(mean), replace
			qui summarize `treatvar1' 		if e(sample)==1
			estadd scalar Treat1 = 			r(mean), replace
			qui summarize `treatvar2' 		if e(sample)==1
			estadd scalar Treat2 = 			r(mean), replace
			estadd local year=				"2015-17" , replace
			estadd local res_same=			"\checkmark", replace
			estadd local work_same=			"\checkmark", replace
			estadd local res_different=		"\checkmark", replace
			estadd local work_different=	"\checkmark", replace
			*eststo table3_`outcome'_reg3
			estimates save "${ster}table3_`outcome'_reg3" , replace

			* (4) Same workplace
			reghdfe `outcome' `treatvar1' `treatvar2' ///
				$age $distance $time $publictime $publicquality ///
				if grk_bed_nochange == 1, ///
				absorb($FE $household $income $employment $education i.year) ///
				vce(cluster $clustervar) 
			qui summarize BEV_fam_yes 		if e(sample)==1
			estadd scalar MeanDep1 = 		r(mean), replace
			qui summarize ICE_fam_count 	if e(sample)==1
			estadd scalar MeanDep2 = 		r(mean), replace
			qui summarize `treatvar1' 		if e(sample)==1
			estadd scalar Treat1 = 			r(mean), replace
			qui summarize `treatvar2' 		if e(sample)==1
			estadd scalar Treat2 = 			r(mean), replace
			estadd local year=				"2015-17" , replace
			estadd local res_same=			"\checkmark", replace
			estadd local work_same=			"\checkmark", replace
			estadd local res_different=		"\checkmark", replace
			estadd local work_different=	"\checkmark", replace
			*eststo table3_`outcome'_reg4
			estimates save "${ster}table3_`outcome'_reg4" , replace

			* (5) Stayers only
			reghdfe `outcome' `treatvar1' `treatvar2' ///
				$age $distance $time $publictime $publicquality ///
				if grk_nochange == 1 & grk_bed_nochange == 1, ///
				absorb($FE $household $income $employment $education i.year) ///
				vce(cluster $clustervar) 
			qui summarize BEV_fam_yes 		if e(sample)==1
			estadd scalar MeanDep1 = 		r(mean), replace
			qui summarize ICE_fam_count 	if e(sample)==1
			estadd scalar MeanDep2 = 		r(mean), replace
			qui summarize `treatvar1' 		if e(sample)==1
			estadd scalar Treat1 = 			r(mean), replace
			qui summarize `treatvar2' 		if e(sample)==1
			estadd scalar Treat2 = 			r(mean), replace
			estadd local year =				"2015-17" , replace
			estadd local res_same =			"\checkmark", replace
			estadd local work_same =		"\checkmark", replace
			estadd local res_different =	"\checkmark", replace
			estadd local work_different =	"\checkmark", replace
			*eststo table3_`outcome'_reg5
			estimates save "${ster}table3_`outcome'_reg5" , replace
		}
	}
}

*===============================================================================
// Step 3: Load ster, save as table
*===============================================================================
*     BEV and ICEV vertically in same table 

eststo drop *
* Baseline regression
estimates use "${ster}table3_BEV_fam_yes_reg1"
eststo 
* Same residence location
estimates use "${ster}table3_BEV_fam_yes_reg2"
eststo
* NEW: Different residence location
estimates use "${ster}table3_BEV_fam_yes_reg3"
eststo
* Same work location
estimates use "${ster}table3_BEV_fam_yes_reg4"
eststo
* Same residence and work location
estimates use "${ster}table3_BEV_fam_yes_reg5"
eststo

* Looking at results on screen
esttab est*, ///
	keep(toll_fam_mean ptl_fam_km_mean) ///
	coeflabels( ///
		toll_fam_mean "Road toll (NOK)" ///
		ptl_fam_km_mean "Bus lane (km)" ///
	) se star(* 0.10 ** 0.05 *** 0.01) 

* === Making part A of table, coefficients for BEVs ============================	
esttab est* ///
	using "${tables}table3_fragmentA.tex", ///
	keep(toll_fam_mean ptl_fam_km_mean) ///
	coeflabels( ///
		toll_fam_mean "Road toll (NOK)" ///
		ptl_fam_km_mean "Bus lane (km)" ///
	) ///	
	fragment nodepvars nonumbers nogaps nolines nonotes noobs nomtitle prehead("") posthead("") stats() ///
	se star(* 0.10 ** 0.05 *** 0.01) replace substitute (\_ _)

* === Making part C of table, summary statistics ===============================
esttab est* ///
	using "${tables}table3_fragmentC.tex", ///
	drop(*) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs nomtitle prehead("") posthead("") ///
	stats(N year res_same work_same res_different work_different MeanDep1 MeanDep2 Treat1 Treat2 ///
	, label("N" "Year" "Same residential location" "Same work location" "Different residential location" "Different work location" "Mean outcome, BEV" "Mean outcome, ICEVs" "Mean road toll (NOK)" "Mean bus lane (km)") fmt(%9.0fc a3)) ///
	se star(* 0.10 ** 0.05 *** 0.01) replace substitute (\_ _)
	
	
* === ICEV
eststo drop *
* Baseline regression
estimates use "${ster}table3_ICE_fam_count_reg1"
eststo 
* Same residence location
estimates use "${ster}table3_ICE_fam_count_reg2"
eststo
* NEW: Different residence location
estimates use "${ster}table3_ICE_fam_count_reg3"
eststo
* Same work location
estimates use "${ster}table3_ICE_fam_count_reg4"
eststo
* Same residence and work location
estimates use "${ster}table3_ICE_fam_count_reg5"
eststo

* Looking at results on screen
esttab est*, ///
	keep(toll_fam_mean ptl_fam_km_mean) ///
	coeflabels( ///
		toll_fam_mean "Road toll (NOK)" ///
		ptl_fam_km_mean "Bus lane (km)" ///
	) se star(* 0.10 ** 0.05 *** 0.01)  

* === Making part B of table, coefficients for BEVs ============================
esttab est* ///
	using "${tables}table3_fragmentB.tex", ///
	keep(toll_fam_mean ptl_fam_km_mean) ///
	coeflabels( ///
		toll_fam_mean "Road toll (NOK)" ///
		ptl_fam_km_mean "Bus lane (km)" ///
	) ///	
	fragment nodepvars nonumbers nogaps nolines nonotes noobs nomtitle prehead("") posthead("") stats() ///
	se star(* 0.10 ** 0.05 *** 0.01) replace substitute (\_ _)	
	
	